package com.yanqu.road.server.manger.activity.silkroad;

import com.yanqu.road.dao.impl.activity.silkroad.SilkRoadConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadEventConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadItemConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadPlaceConfig;
import com.yanqu.road.entity.activity.silkroad.config.SilkRoadSeaConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.silkroad.SilkRoadConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.game.service.GameService;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.silkroad.SilkRoadModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.silkroad.service.SilkRoadLookOtherReflicServiceCmd;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.RandomHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SilkRoadMgr extends TempMgr {
    private static ActivityInfo activityInfo;

    /**
     * 配置
     */
    private static SilkRoadConfig config;

    private static RandomHelper random = new RandomHelper();


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void initService(GameService syncMessageService) {
        syncMessageService.addGameServiceCommand(GameProtocol.S_SILK_ROAD_LOOK_OTHER_RELIC, new SilkRoadLookOtherReflicServiceCmd());
    }

    public static void reloadActivityInfo() {
        getLogger().info("reload silkroad Activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SilkRoad.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no silkroad Activity in show time");
            activityInfo = null;
            config = null;
        } else {
            ActivityInfo tempActivityInfo = openActivityInfoList.get(0);
            List<Integer> activityIdList = new ArrayList<>();
            int activityId = tempActivityInfo.getActivityId();
            activityIdList.add(activityId);
            Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityId);
            SilkRoadConfig tempConfig = new SilkRoadConfig();
            tempConfig.setActivityInfo(tempActivityInfo);
            tempConfig.setPlaceList(new SilkRoadConfigDaoImpl().getSilkRoadPlaceList(activityId));
            tempConfig.setEventMap(new SilkRoadConfigDaoImpl().getSilkRoadEventMap(activityId));
            tempConfig.setSeaConfigMap(new SilkRoadConfigDaoImpl().getSilkRoadSeaMap(activityId));
            tempConfig.setSilkRoadItemMap(new SilkRoadConfigDaoImpl().getSilkRoadItemMap(activityId));
            tempConfig.parseConfig(activityConfigMap);

            activityInfo = tempActivityInfo;
            config = tempConfig;
        }
        playerReloadActivity();
        getLogger().info("reload silkroad Activity finish");
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        for (GamePlayer onlinePlay : playerList) {
            onlinePlay.getModule(SilkRoadModule.class).initUserData();
            onlinePlay.getModule(SilkRoadModule.class).syncConfigMsg();
            onlinePlay.getModule(SilkRoadModule.class).syncUserMsg();
        }
    }

    public static boolean isInActivityShowTime() {
        if (config == null) {
            return false;
        }
        return ActivityMgr.activityInShowTime(config.getActivityInfo());
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static SilkRoadConfig getConfig() {
        return config;
    }

    /**
     * 初始配置消息体
     */
    public static SilkRoadProto.SilkRoadConfigListMsg.Builder initConfigMsg(String language) {
        SilkRoadProto.SilkRoadConfigListMsg.Builder builder = SilkRoadProto.SilkRoadConfigListMsg.newBuilder();
        SilkRoadProto.SilkRoadConfig.Builder configBuilder = SilkRoadProto.SilkRoadConfig.newBuilder();
        configBuilder.setActivityId(activityInfo.getActivityId());
        for (SilkRoadPlaceConfig silkRoadPlaceConfig : config.getPlaceList()) {
            SilkRoadProto.SilkRoadPlaceConfigTemp.Builder placeBuilder = SilkRoadProto.SilkRoadPlaceConfigTemp.newBuilder();
            placeBuilder.setId(silkRoadPlaceConfig.getId());
            placeBuilder.setName(ServerLanguageMgr.getContent(silkRoadPlaceConfig.getName(), language));
            placeBuilder.setDesc(ServerLanguageMgr.getContent(silkRoadPlaceConfig.getDesc(), language));
            placeBuilder.setIcon(silkRoadPlaceConfig.getIcon());
            placeBuilder.setPlaceCoordinate(silkRoadPlaceConfig.getPlaceCoordinate());
            placeBuilder.setRouteCoordinate(silkRoadPlaceConfig.getRouteCoordinateLists());
            placeBuilder.setEventDesc(ServerLanguageMgr.getContent(silkRoadPlaceConfig.getEventDesc(), language));
            placeBuilder.setRewards(silkRoadPlaceConfig.getRewards());
            placeBuilder.setSpecialRewards(silkRoadPlaceConfig.getSpecialRewards());

            configBuilder.addPlaceConfigTemp(placeBuilder);
        }

        for (SilkRoadEventConfig roadEvent : config.getEventMap().values()) {
            SilkRoadProto.SilkRoadEventConfigTemp.Builder eventBuilder = SilkRoadProto.SilkRoadEventConfigTemp.newBuilder();
            eventBuilder.setId(roadEvent.getId());
            eventBuilder.setEventType(roadEvent.getEventType());
            eventBuilder.setIcon(roadEvent.getIcon());
            eventBuilder.setRewards(roadEvent.getRewards());
            eventBuilder.setSpecialRewards(roadEvent.getSpecialRewards());
            eventBuilder.setEventDesc(ServerLanguageMgr.getContent(roadEvent.getEventDesc(), language));

            configBuilder.addEventConfigTemp(eventBuilder);
        }

        for (Map<Integer, SilkRoadSeaConfig> roadSeaMap : config.getSeaConfigMap().values()) {
            for (SilkRoadSeaConfig roadSea : roadSeaMap.values()) {
                SilkRoadProto.SilkRoadSeaConfigTemp.Builder seaBuilder = SilkRoadProto.SilkRoadSeaConfigTemp.newBuilder();
                seaBuilder.setType(roadSea.getType());
                seaBuilder.setItemId(roadSea.getId());
                seaBuilder.setWeights(roadSea.getWeights());
                seaBuilder.setNum(roadSea.getRandmoNum());
                seaBuilder.setIcon(roadSea.getIcon());
                seaBuilder.setQuality(roadSea.getQuality());
                seaBuilder.setCoordinate(roadSea.getCoordinate());
                seaBuilder.setRange(roadSea.getRange());
                seaBuilder.setMoveType(roadSea.getMoveType());
                seaBuilder.setMoveRange(roadSea.getMoveRange());
                seaBuilder.setSpeed(roadSea.getSpeed());
                seaBuilder.setName(ServerLanguageMgr.getContent(roadSea.getName(), language));
                seaBuilder.setRetractSpeed(roadSea.getRetractSpeed());
                seaBuilder.setMapId(roadSea.getMapId());
                seaBuilder.setRewards(roadSea.getRewards());

                configBuilder.addSeaConfigTemp(seaBuilder);
            }
        }

        for (SilkRoadItemConfig roadItem : config.getSilkRoadItemMap().values()) {
            SilkRoadProto.SilkRoadItemConfigTemp.Builder itemBuilder = SilkRoadProto.SilkRoadItemConfigTemp.newBuilder();
            itemBuilder.setId(roadItem.getId());
            itemBuilder.setName(ServerLanguageMgr.getContent(roadItem.getName(), language));
            itemBuilder.setDesc(ServerLanguageMgr.getContent(roadItem.getDesc(), language));
            itemBuilder.setIcon(roadItem.getIcon());
            itemBuilder.setQuality(roadItem.getQuality());
            itemBuilder.setAchievement(roadItem.getAchievementRewards());
            itemBuilder.setIsAnimation(roadItem.getIsAnimation());

            configBuilder.addItemConfigTemp(itemBuilder);
        }


        builder.addConfig(configBuilder);

        config.initConfigMsg(builder, language);

        return builder;
    }

    public static SilkRoadProto.SilkRoadConfigListMsg.Builder getConfigMsg(String language) {
        SilkRoadProto.SilkRoadConfigListMsg.Builder configMsg = config.getConfigMsg(language);
        if (configMsg == null) {
            synchronized (config){
                configMsg = config.getConfigMsg(language);
                if (configMsg == null) {
                    configMsg = initConfigMsg(language);
                }
            }
        }
        return configMsg;
    }

    /**
     * 获得最后一个大地点的配置
     */
    public static SilkRoadPlaceConfig getLastPlaceConfig() {
        return config.getPlaceList().get(config.getPlaceList().size() - 1);
    }
}
